<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="请选择下拉选择" filterable clearable :style="{ width: '100%' }">
          <el-option v-for="(item, index) in companyList" :key="index" :label="item.companyName"
            :value="item.companyId" />
        </el-select>
      </el-form-item>

      <el-form-item label="预约项目" prop="bookingProject">
        <el-input v-model="queryParams.bookingProject" placeholder="请输入预约项目" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:booking:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:booking:export']">导出</el-button>
      </el-col>
      <audio controls="controls" hidden src="../voice/zhendong.mp3" ref="audio1"></audio>
      <audio controls="controls" hidden src="../voice/tips.mp3" ref="audio2"></audio>
      <audio controls="controls" hidden src="../voice/delete.mp3" ref="audio3"></audio>
      <audio controls="controls" hidden src="../voice/Download.mp3" ref="audio4"></audio>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约服务序号" align="center" type="index" width="150" />
      <el-table-column label="预约公司" align="center" prop="companyName" />
      <el-table-column label="预约类型" align="center" prop="bookingType" />
      <el-table-column label="预约项目" align="center" prop="bookingProject" />
      <el-table-column label="预约时间" align="center" prop="bookingTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="b_status">
        <template slot-scope="scope">
          <span style="color: #1890ff; font-size: 12px;" >
            {{ scope.row.bStatus == 1 ? '已过期' : (scope.row.bStatus == 2 ? '进行中' : '未过期') }}
          </span>
          
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:booking:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改预约公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约公司" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择下拉选择" filterable clearable :style="{ width: '100%' }">
            <el-option v-for="(item, index) in companyList" :key="index" :label="item.companyName"
              :value="item.companyId" />
          </el-select>

        </el-form-item>
        <el-form-item label="预约项目" prop="bookingProject">
          <el-input v-model="form.bookingProject" placeholder="请输入预约项目" />
        </el-form-item>
        <el-form-item label="预约时间" prop="bookingTime">
          <el-date-picker clearable v-model="form.bookingTime" :picker-options="pickerOptions" @change="dateName()"
            type="date" value-format="yyyy-MM-dd" placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBooking, getBooking, delBooking, addBooking, updateBooking } from "@/api/system/booking";
import { listCompany } from "@/api/system/company";
import { companyList } from "@/api/system/company";
export default {
  name: "Booking",
  data() {
    return {
      // 遮罩层
      loading: true,
      dataPicker: new Date() - 24 * 60 * 60 * 1000, //默认显示昨天时间
      pickerOptions: { //控制时间范围
        disabledDate(time) {
          return time.getTime() < (Date.now() - (24 * 60 * 60 * 1000))
        }
      },

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      bookingList: [],
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        bookingType: null,
        bookingProject: null,
        bookingTime: null,
        fakeDelete: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookingProject: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCompany()
  },

  methods: {
    /** 查询预约列表 */
    getList() {
      this.loading = true;
      listBooking(this.queryParams).then(response => {
        this.bookingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCompany() {
      companyList(this.queryParams).then(response => {
        this.companyList = response.rows;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookingId: null,
        companyId: null,
        bookingType: null,
        bookingProject: null,
        bookingTime: null,
        fakeDelete: null,
        endTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookingId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增公司预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      this.reset();
      const bookingId = row.bookingId || this.ids
      getBooking(bookingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookingId != null) {
            updateBooking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBooking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime = 0;
      this.$refs.audio3.play();
      const bookingIds = row.bookingId || this.ids;
      if (row.bStatus == 1) {
        this.$modal.confirm('是否确认删除预约编号为"' + bookingIds + '"的数据项？').then(function () {
          return delBooking(bookingIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }else{
        this.$modal.msgWarning('预约未到期,不可删除');
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      this.$refs.audio4.currentTime = 0;
      this.$refs.audio4.play();
      this.download('system/booking/export', {
        ...this.queryParams
      }, `booking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
